<?php
include_once(dirname(__FILE__) . "/../inc/common.php");
$method = isset($_GET["method"]) ? $_GET["method"] : "";

$flowClass = new tally_flow();

$excludeTagIds = explode(",", "38");
$excludeCategoryIds = explode(",", "31,28,52,20");
$excludeMarks = explode(",", "vip");

$tallyTagClass = new tally_tag();
$excludeTags = $tallyTagClass->findTagByIds($userId, $excludeTagIds);

$tallyFlowTypeClass = new tally_flowType();
$excludeCategories = $tallyFlowTypeClass->findFlowTypeByIds($userId, $excludeCategoryIds);

function _array_column($list, $element)
{
    if (!isset($list) || !is_array($list) || empty($list)) {
        return array();
    }
    if (!isset($element) || empty($element)) {
        return array();
    }
    $j = 0;
    $result = array();
    for ($i = 0; $i < count($list); $i++) {
        if (isset($list [$i] [$element])) {
            $result [$j] = $list [$i] [$element];
            $j++;
        } else {
            return array();
        }
    }
    return $result;
}

if ($method == "pole") {
    // 所时间段每月收入与支出
    $lastMonthNumber = isset($_POST["lastMonthNumber"]) && $_POST["lastMonthNumber"] ? $_POST["lastMonthNumber"] : 24;

    $categories = array();
    $assetList = array();

    $currentMonth = date("n");
    $currentYear = date("Y");
    for ($i = $lastMonthNumber; $i > 0; $i--) {
        $endTime = strtotime($currentYear . "-" . $currentMonth . "-01 00:00:00");
        $currentMonth--;
        if ($currentMonth <= 0) {
            $currentYear--;
            $currentMonth = 12;
        }
        $startTime = strtotime($currentYear . "-" . $currentMonth . "-01 00:00:00");

        if(sizeof($categories)===0){
          // 最后一个月显示当前时间
          $endTime = time();
        }
        $categories[] = date("Y-m-d", $endTime-1);
        $assetList[] = $flowClass->getEndBalanceByTimeRange($userId, $endTime);
    }

    echo json_encode(array(
        "categories" => $categories,
        "series" => array(
            array(
                "name" => '资产',
                "data" => $assetList

            )
        ),
    ));
} else {
    ?>
    <!-- begin:导航条 -->
    <div class="titlebar">
        <h3>
            按月资产统计
        </h3>
    </div>
    <!-- end:导航条 -->

    <div class="tab_wrapper">
        <div class="body">
            <!-- begin:搜索区 -->
            <form id="schForm" name="schForm" method="post">
                <div class="searchbar" id="searchbar" style="line-height: 150%; display: block;">
                    <div class="base_condition">
                        <table>
                            <tr>
                                <td>
                                    最近<INPUT TYPE="text" id="lastMonthNumber" NAME="lastMonthNumber" value="24">个月
                                </td>
                                <td><input id="searchBtn" class="button" type="button" value="查询"
                                           onclick="Report.searchChart();"/>
                                </td>
                            </tr>
                        </table>
                    </div>

                </div>
            </form>
            <!-- end:搜索区 -->

            <div class="report_bar">
                <span>数据图表</span>
            </div>

            <div id="chartView" class="chartView">
                <div align="center">
                    <img align="absmiddle" src='/images/common/loading1.gif'>
                </div>
            </div>


            <!-- end:---------主数据区 -->
        </div>
    </div>
    <?php
}
?>
